<!-- head -->
{include file='public/head' /}
<!-- head -->

<!-- left -->
{include file='public/left' /}
<!-- left -->

<!-- Content Wrapper. Contains page content -->
<div class="content-wrapper">
    <!-- Content Header (Page header) -->
    <section class="content-header">
        <h1>
            管理后台
            <small>1.0.0</small>
        </h1>
        <ol class="breadcrumb">
        </ol>
    </section>

    <!-- Main content -->
    <section class="content">
        <div class="row">
            <div class="col-sm-12">
                <div class="box box-default">
                    <div class="box-header with-border">
                        <h3 class="box-title">编辑分站城市</h3>
                    </div>
                    <form class="form-horizontal" id="form" action="{:url('admin/webcity/postsave','','')}">
                        <input name="id" type="hidden" id="id" value="{$id}">
                        <input name="province_id_value" type="hidden" id="province_id_value" value="{$row.province_id}">
                        <input name="city_id_value" type="hidden" id="city_id_value" value="{$row.city_id}">
                        <div class="box-body">
                            <div class="form-group">
                                <label for="province_id" class="col-sm-2 control-label">选择城市</label>
                                <div class="col-sm-5">
                                    <select class="form-control" id="province_id" name="province_id">
                                        <option value="0">请选择</option>
                                        {volist name='provinceList' id='province'}
                                        <option value="{$province.id}" {if condition="$row.province_id == $province.id"}selected{/if}>{$province.name}</option>
                                        {/volist}
                                    </select>
                                    <span class="help-block" style="color:#f00;"></span>
                                </div>
                                <div class="col-sm-5">
                                    <select class="form-control" id="city_id" name="city_id">
                                        <option value="0">请选择</option>
                                    </select>
                                </div>
                            </div>
                        </div>
                        <div class="box-footer" style="text-align:right;">
                            <button type="button" class="btn btn-sm btn-default" id="resetForm">清空</button>&nbsp;
                            <button type="button" class="btn btn-sm btn-primary" id="saveForm">保存</button>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </section>
    <!-- /.content -->
</div>
<!-- /.content-wrapper -->

<script type="text/javascript">



    $(function(){
        var province_id_value = $('#province_id_value').val();
        var city_id_value = $('#city_id_value').val();
        $.ajax({
            url: "{:url('admin/webcity/getcity','','')}",
            type: 'GET',
            data: {
                'id': province_id_value,
            },
            dataType: 'JSON',
            success: function(data) {
                if (data.status == 1) {
                    var cityList = data.data;
                    var optionString = '<option value="0">请选择</option>';
                    for (var i = 0; i < cityList.length; i++) {
                        optionString = optionString + '<option value="' + cityList[i].id + '">' + cityList[i].name + '</option>';
                    }
                    $('#city_id').html(optionString);
                    $('#city_id').find('option[value="' + city_id_value + '"]').prop('selected', 'selected');
                }
            }
        });

        $('#province_id').on('change', function (a) {
            var provinceId = $('#province_id').val();
            $.ajax({
                url: "{:url('admin/webcity/getcity','','')}",
                type: 'GET',
                data: {
                    'id': provinceId,
                },
                dataType: 'JSON',
                success: function(data) {
                    if (data.status == 1) {
                        var cityList = data.data;
                        var optionString = '<option value="0">请选择</option>';
                        for (var i = 0; i < cityList.length; i++) {
                            optionString = optionString + '<option value="' + cityList[i].id + '">' + cityList[i].name + '</option>';
                        }
                        $('#city_id').html(optionString);
                    }
                }
            });
        });

        $('#saveForm').click(function(){
            var id = $('#id').val();
            var province_id = $('#province_id').val();

            var city_id = $('#city_id').val();
            if (city_id == 0) {
                $('#province_id').parents('.form-group').addClass('has-error');
                $('#province_id').parent().find('.help-block').html('请选择城市');
                return false;
            }

            $.ajax({
                url: $('#form').attr('action'),
                type: 'POST',
                data: {
                    'id': id,
                    'province_id': province_id,
                    'city_id': city_id,
                    'city': $('#city_id option:selected').text(),
                },
                dataType: 'JSON',
                success: function(data) {
                    if (data.status == 1) {
                        window.location.href = "{:url('admin/webcity/index','','')}";
                    } else {
                        showAlert('提示', data.info, null);
                    }
                }
            });
        });

        $('#province_id, #city_id').focus(function(){
            if ($('#province_id').parents('.form-group').hasClass('has-error')) {
                $('#province_id').parents('.form-group').removeClass('has-error');
                $('#province_id').parent().find('.help-block').html('');
            }
        });

    });

</script>

<!-- right -->
{include file='public/right' /}
<!-- right -->

<!-- foot -->
{include file='public/foot' /}
<!-- foot -->
